package com.example.train.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.train.common.context.LoginMemberContext;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.example.train.member.domain.Passenger;
import com.example.train.member.domain.PassengerExample;
import com.example.train.member.mapper.PassengerMapper;
import com.example.train.member.req.PassengerQueryReq;
import com.example.train.member.req.PassengerSaveReq;
import com.example.train.member.resp.PassengerQueryResp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: invictusQAQ
 * @Date: 2023-09-21-16:46
 * @Description:
 */
@Service
public class PassengerService {

    private static final Logger LOG = LoggerFactory.getLogger(PassengerService.class);

    @Resource
    private PassengerMapper passengerMapper;

    /**
     * 添加乘车人及修改
     * @param req
     * @return void
     **/
    public void save(PassengerSaveReq req){
        DateTime now = DateTime.now();
        Passenger passenger= BeanUtil.copyProperties(req,Passenger.class);
        //若ID为空说明是第一次添加
        if(ObjUtil.isNull(passenger.getId())){
            passenger.setMemberId(LoginMemberContext.getId());
            passenger.setId(SnowUtil.getSnowflakeNextId());
            passenger.setCreateTime(now);
            passenger.setUpdateTime(now);
            passengerMapper.insert(passenger);
        }else{
            passenger.setUpdateTime(now);
            passengerMapper.updateByPrimaryKey(passenger);
        }
    }

    public PageResp<PassengerQueryResp> queryList(PassengerQueryReq req){
        PassengerExample passengerExample=new PassengerExample();
        PassengerExample.Criteria criteria= passengerExample.createCriteria();
        //若MemberId不存在说明为控台操作，可查询所有乘车人
        if(ObjectUtil.isNotNull(req.getMemberId())){
            criteria.andMemberIdEqualTo(req.getMemberId());
        }
        LOG.info("查询页码：{}", req.getPage());
        LOG.info("每页条数：{}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Passenger> passengerList=passengerMapper.selectByExample(passengerExample);
        //获取分页查询的passengerList分页数和总记录数
        PageInfo<Passenger> pageInfo=new PageInfo<>(passengerList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<PassengerQueryResp> list=BeanUtil.copyToList(passengerList, PassengerQueryResp.class);

        PageResp<PassengerQueryResp> pageResp=new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    public void delete(Long id){
        PassengerExample passengerExample=new PassengerExample();
        //校验只有该乘车人的添加人才有资格删除
        passengerExample.createCriteria().andMemberIdEqualTo(LoginMemberContext.getId())
                .andIdEqualTo(id);
        passengerMapper.deleteByExample(passengerExample);
    }

    /**
     * 查询我的所有乘客
     */
    public List<PassengerQueryResp> queryMine(){
        PassengerExample passengerExample=new PassengerExample();
        passengerExample.setOrderByClause("name asc");
        PassengerExample.Criteria criteria= passengerExample.createCriteria();
        criteria.andMemberIdEqualTo(LoginMemberContext.getId());
        List<Passenger> list=passengerMapper.selectByExample(passengerExample);
        return BeanUtil.copyToList(list,PassengerQueryResp.class);
    }
}
